//
//  MainTableHeaderView.m
//  ProjectforHDX
//
//  Created by Jaesun on 16/6/30.
//  Copyright © 2016年 H.DX. All rights reserved.
//

#import "MainTableHeaderView.h"
#import "StockInquiryVC.h"

#define   HEADER_HEIGHT 250

#define  MREO_HEIGHT 35
#define   BTN_HEIGHT 90

@interface MainTableHeaderView()


@property(nonatomic,strong) NSMutableArray  *ArrayValue;//按钮文字

@property(nonatomic,strong) NSArray * typeArray;//数据数组
@end
@implementation MainTableHeaderView

-(id)initHeaderView:(NSArray*)typeArray{
    self=[super initWithFrame:CGRectMake(0, 0, BOUNDSWIDTH, HEADER_HEIGHT)];
    if(self){
        self.typeArray=typeArray;
        
        self.backgroundColor= [UtilCheck hexStringToColor:@"1F62D2" alpha:1];
       
        [self createTypeView];
        [self createBtnView];
        [self createMoreView];
    }
    return self;
}



-(void)setValueArray:(NSArray*)array{
    
    for(int i=0;i<array.count;i++){
        
        if(i<self.ArrayValue.count){
            NSDictionary *dict=[array objectAtIndex:i];
            RZCustomLable *labValue=(RZCustomLable*)self.ArrayValue[i];
            labValue.text=[NSString stringWithFormat:@"%@",[dict objectForKey:@"count"]];
        }
        
    }
}



/**
 *  创建新建、库存查询按钮
 */
-(void)createTypeView{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height -BTN_HEIGHT-MREO_HEIGHT)];
    
    [view setBackgroundColor:[UtilCheck getRZColor:35 green:102 blue:207 alpha:1]];
    
    [self addSubview:view];
    THUserInfoModel *userModel=[THMangerTool getAccountInfo];
    
    if (![userModel.operationPermissions isKindOfClass:[NSArray class]]) {
        return;
    }
    NSInteger count =userModel.operationPermissions.count;
    CGFloat width=(view.width-(count+1) * 10)/(CGFloat)count;

    for(int i=0;i<count;i++){
        
        UIImage *image=BDFILEIMAGENAEM(@"Home/btn_addOrder");
        NSString *title=[THMangerTool language:@"main.orderCreate"];
        NSInteger type=[userModel.operationPermissions[i] intValue];
        NSLog(@"操作权限是%ld",(long)type);
        
        switch (type) {
            case 1:
            {
                image=BDFILEIMAGENAEM(@"Home/btn_addOrder");
                title=[THMangerTool language:@"main.orderCreate"];
                
            }
                break;
            case 2:
            {
                
                image=BDFILEIMAGENAEM(@"Home/btn_updateOrder");
                title=[THMangerTool language:@"main.orderUpdate"];
            }
                break;
            case 3:
            {
                
                image=BDFILEIMAGENAEM(@"Home/btn_query");
                title=[THMangerTool language:@"main.orderQuery"];
            }
                break;
            case 4:
            {
                
                image=BDFILEIMAGENAEM(@"Home/btn_inventory");
                title=[THMangerTool language:@"main.orderInventory"];
            }
                break;
            case 5:
            {
                
                image= [UIImage imageNamed:@"智能柜"];
                title=[THMangerTool language:@"main.cunqunjian"];
            }
                break;
                
            default:
                break;
        }
        
        
        OperationView *optionView=[[OperationView alloc] initOperationFrame:CGRectMake(10*(i+1)+width*i,10, width+10, view.height-20) icon:image title:title ];
 
        optionView.tag=200+type;
        UITapGestureRecognizer *tapv=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView:)];
        tapv.view.tag=optionView.tag;
        [optionView addGestureRecognizer:tapv];
        [view addSubview:optionView];
 
    }
    
    
}
/**
 *  创建中间的按钮
 */
-(void)createBtnView{
    self.ArrayValue=[[NSMutableArray alloc] initWithCapacity:0];
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, self.height-BTN_HEIGHT-MREO_HEIGHT, self.width, BTN_HEIGHT)];
    [view setBackgroundColor:[UtilCheck hexStringToColor:@"ffffff" alpha:1]];
    [self addSubview:view];
    
    NSInteger count =self.typeArray.count;
    
    CGFloat width=view.width/(CGFloat)count+0.5;
    
    for(int i=0;i<count;i++){
        NSDictionary *dict=[self.typeArray objectAtIndex:i];
        
        NSString *title=[NSString stringWithFormat:@"%@",[dict objectForKey:@"typeName"]];
        NSString *count=[NSString stringWithFormat:@"%@",[dict objectForKey:@"count"]];
        NSInteger type=[[dict objectForKey:@"type"] intValue];
        UIImage *image=BDFILEIMAGENAEM(@"Home/img_undoneOrder");
        switch (type) {
 
            case 1:
            {
               image=BDFILEIMAGENAEM(@"Home/img_undoneOrder");
            }
                break;
                
            case 2:
            {
                image=BDFILEIMAGENAEM(@"Home/img_waitDeliver");
            }
                break;
                
            case 3:
            {
               image=BDFILEIMAGENAEM(@"Home/img_waitReceive");
            }
                break;
                
            case 4:
            {
                image=BDFILEIMAGENAEM(@"Home/img_watiBack");
            }
                break;
            case 5:
            {
              image= [UIImage imageNamed:@"智能柜"];
            }
                break;
                
            default:
                break;
        }
        
        ItemButtonView *itemView=[[ItemButtonView alloc] initItemFrame:CGRectMake(width * i, 0, width, view.height) icon:image title:title value:count];
        
        [self.ArrayValue addObject:itemView.labValue];
        itemView.tag=100+type;
        UITapGestureRecognizer *tapv=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView:)];
        tapv.view.tag=itemView.tag;
        [itemView addGestureRecognizer:tapv];
        [view addSubview:itemView];
        
        if(i<=self.typeArray.count){
            [itemView addSoldLine:0.5 soldcolor:[UtilCheck hexStringToColor:@"e5e5e5" alpha:1]  pointA:CGPointMake(itemView.width, 10) pointB:CGPointMake(itemView.width, itemView.height-10)];
        }
    }
    
}

-(void)tapView:(UITapGestureRecognizer*)recognizer{
   
    switch (recognizer.view.tag-101) {
        case 0:
        {
            if(self.viewblock){
                self.viewblock(NSButtonCustomerNoComplete);
            }
        }
            break;
        case 1:
        {
            if(self.viewblock){
                self.viewblock(NSButtonCustomerNoDelivery);
            }
        }
            break;
        case 2:
        {
            if(self.viewblock){
                self.viewblock(NSButtonCustomerNoReceipt);
            }
        }
            break;
        case 3:
        {
            if(self.viewblock){
                self.viewblock(NSButtonCustomerNoParts);
            }
        }
            break;
       
        case 4:
        {
            if(self.viewblock){
                self.viewblock(NSButtonCustomerNoSure);
            }
        }
             break;
        case 104:
        {
            if(self.viewblock){
                self.viewblock(NSButtonCustomerSmartCabinet);
            }
        }
            break;
            
        case 100:
        {
            if(self.viewblock){
                self.viewblock(NSButtonOrderCreate);
            }
        }
            break;
        case 101:
        {
            if(self.viewblock){
                self.viewblock(NSButtonOrderUpdate);
            }
        }
            break;
        case 102:
        {
            if(self.viewblock){
                self.viewblock(NSButtonOrderQuery);
            }
        }
            break;
        case 103:
        {
            if(self.viewblock){
                self.viewblock(NSButtonOrderInventory);
            }
        }
            break;
        default:
            break;
    }
    
}

-(void)createMoreView{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, self.height-MREO_HEIGHT, self.width, MREO_HEIGHT)];
    
    UILabel *UpLine = [[UILabel alloc]init];
    [UpLine setFrame:CGRectMake(0, 0, self.width, 0.5)];
    [UpLine setBackgroundColor:[Toolobject getColorByHexString:@"#e5e5e5" alpha:1]];
    [view addSubview:UpLine];
    
    
  
    [view setBackgroundColor:[UtilCheck getRZColor:244 green:248 blue:251 alpha:1]];
    
    RZCustomButtom *submit=[[RZCustomButtom alloc] init];
    [submit setFrame:CGRectMake(10,0,120, view.height)];
    [submit setTitle:[NSString stringWithFormat:@" %@",[THMangerTool language:@"main.orderLately"]] forState:UIControlStateNormal];//确认
    submit.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [submit setImage:BDFILEIMAGENAEM(@"Home/img_lastOrder") forState:UIControlStateNormal];
    [submit.titleLabel setFont:CustomFont(14)];
    [submit setTitleColor:[UtilCheck hexStringToColor:@"48555e" alpha:1]  forState:UIControlStateNormal];
    [view addSubview:submit];
    
    RZCustomButtom *deliveryReport=[[RZCustomButtom alloc] init];
    [deliveryReport setFrame:CGRectMake(view.width/2-50,0,100, view.height)];
    [deliveryReport setTitle:[NSString stringWithFormat:@" %@",[THMangerTool language:@"main.shipmentReport"]] forState:UIControlStateNormal];//确认
    deliveryReport.titleLabel.textAlignment = UITextAlignmentCenter;
    [deliveryReport.titleLabel setFont:CustomFont(14)];
    deliveryReport.tag=101;
    [deliveryReport setTitleColor:[UtilCheck hexStringToColor:@"1f62d2" alpha:1]  forState:UIControlStateNormal];
    [deliveryReport addTarget:self action:@selector(deliveryReport:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:deliveryReport];
    
    RZCustomButtom *btnmore=[[RZCustomButtom alloc] init];
    [btnmore setFrame:CGRectMake(view.width-70,0,60, view.height)];
    [btnmore setTitle:[NSString stringWithFormat:@" %@",[THMangerTool language:@"main.more"]] forState:UIControlStateNormal];//确认
    btnmore.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    btnmore.tag=101;
    [btnmore setTitleColor:[UtilCheck hexStringToColor:@"1f62d2" alpha:1]  forState:UIControlStateNormal];
    [btnmore addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btnmore.titleLabel setFont:CustomFont(13)];
    [view addSubview:btnmore];
    
    
    [self addSubview:view];
}
- (void)deliveryReport:(RZCustomButtom*)sender {
    switch (sender.tag) {
        case 101:
        {
            if(self.viewblock){
                self.viewblock(NSButtonShipmentReport);
            }
        }
            break;
            
        default:
            break;
    }
}
-(void)btnClick:(RZCustomButtom*)sender{
    switch (sender.tag) {
        case 101:
        {
            if(self.viewblock){
                self.viewblock(NSButtonMore);
            }
        }
            break;
 
        default:
            break;
    }
}

@end
